<template>
  <c-dialog ref="dialog" :data="selData" single title="处理" @open="openHandle" @close="closeHandle">
    <c-list ref="form" :model="formData" :rules="formRules" lable-width="120px">
      <c-list-item label="确认结果" prop="customerConfirmResult">
        <c-dict-select v-model="formData.customerConfirmResult" dict-type-code="BOOLEANS" />
      </c-list-item>
      <c-list-item v-if="formData.customerConfirmResult === '1'" label="施工单位" prop="constUnitCode">
        <el-select v-model="unit" value-key="constUnitCode" filterable clearable @change="changeHandle">
          <el-option v-for="(l,i) in list" :key="i" :value="l" :label="`${l.unitName}-${l.principal} ( ${l.principalPhone} )`" />
        </el-select>
      </c-list-item>
      <c-list-item label="确认意见" prop="customerConfirmAdvice">
        <el-input v-model="formData.customerConfirmAdvice" type="textarea" :autosize="{minRows: 3}" maxlength="1024" show-word-limit placeholder="确认意见" clearable />
      </c-list-item>
      <c-list-item>
        <div class="btn-container">
          <c-button @click="cancelHandle">取消</c-button>
          <c-button type="primary" @click="confirmHandle">确认</c-button>
        </div>
      </c-list-item>
    </c-list>

  </c-dialog>
</template>
<script>
import { postGetMaxBatch, postUnitBydelegateBatch, postConfirmUnit, postAnotherUnit } from '@/api/decorate'
import { otherFiltersItem } from '@/utils'
export default {
  name: 'Confirm',
  props: {
    selData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      list: [],
      unit: {},
      formData: {
        customerConfirmResult: '',
        constUnitCode: '',
        unitName: '',
        customerConfirmAdvice: ''
      },
      formRules: {
        customerConfirmResult: [{ required: true, message: '请选择确认结果', trigger: 'change' }],
        constUnitCode: [{ required: true, message: '请选择施工单位', trigger: 'change' }],
        customerConfirmAdvice: [{ required: true, message: '请输入确认意见', trigger: 'change' }]
      }
    }
  },
  computed: {
    detail() {
      return this.selData.length === 0 ? {} : this.selData[0]
    }
  },
  methods: {
    openHandle() {
      this.getDetail()
    },
    getDetail() {
      const { decorationTaskCode } = this.detail
      postGetMaxBatch(decorationTaskCode).then(res => {
        postUnitBydelegateBatch(decorationTaskCode, res.data).then(res => {
          this.list = res.data.records
        })
      })
    },
    changeHandle(v) {
      if (v) {
        this.$set(this.formData, 'constUnitCode', v.constUnitCode)
        this.$set(this.formData, 'unitName', v.unitName)
      } else {
        this.$set(this.formData, 'constUnitCode', '')
        this.$set(this.formData, 'unitName', '')
      }
    },
    cancelHandle() {
      this.$refs.dialog.visible = false
    },
    closeHandle() {
      this.unit = {}
    },
    confirmHandle() {
      this.$refs.form.validate().then(() => {
        if (this.formData.customerConfirmResult === '1') {
          const data = {
            ...this.formData,
            decorationTaskCode: this.detail.decorationTaskCode, // 装修任务编号
            id: this.unit.id // ID
          }
          postConfirmUnit(data).then(res => {
            this.$message.success('操作成功！')
            this.$emit('on-success')
            this.$refs.dialog.visible = false
          })
        } else {
          for (const un of this.list) {
            un.customerConfirmAdvice = this.formData.customerConfirmAdvice
            un.customerConfirmResult = 0
          }
          return postAnotherUnit(this.list, this.detail.decorationTaskCode).then(res => {
            this.$message.success('操作成功！')
            this.$emit('on-success')
            this.$refs.dialog.visible = false
          })
        }
      })
    }
  }
}
</script>
